package com.itheima.blog.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.blog.util.UserThreadLocalUtils;
import com.itheima.blog.web.domain.po.TAlbum;
import com.itheima.blog.web.domain.po.TBlog;
import com.itheima.blog.web.domain.query.BlogPageQuery;
import com.itheima.blog.web.service.ITAlbumService;
import com.itheima.blog.web.domain.query.BlogPageQuery;
import com.itheima.blog.web.service.ITBlogService;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 博客文章表 前端控制器
 * </p>
 *
 * @author sxm
 * @since 2023-07-11
 */
@RestController
@RequestMapping("/owner")
@RequiredArgsConstructor
@Api(tags = "我的博客的相关接口")
public class TBlogController {
    private final ITBlogService itBlogService;
    private final ITAlbumService    itAlbumService;


    @PostMapping("/blog/blogs")
    @ApiOperation("分页查询")
    public R<PageResDto> getBlogPages(@RequestBody BlogPageQuery blogPageQuery) {
        PageResDto pageResDto = itBlogService.getBlogPages(blogPageQuery);
        return R.ok(pageResDto);
    }

    @PostMapping("/blog")
    @ApiOperation("新增博客")
    public R save(@RequestBody TBlog tBlog) {
        tBlog.setStatus(false);
        return itBlogService.save(tBlog) ? R.ok() : R.error("新增失败");
    }

    @GetMapping("/blog/{blogId}")
    @ApiOperation("根据id查询博客")
    public R<TBlog> getBlogById(@PathVariable String blogId) {
        return R.ok(itBlogService.getById(blogId));
    }

    @PutMapping("/blog/{blogId}")
    @ApiOperation("编辑博客")
    public R updateBlog(@PathVariable Long blogId
            , @RequestBody TBlog tblog) {
        tblog.setId(blogId);
        return itBlogService.updateById(tblog) ? R.ok() : R.error("修改失败");
    }


    @DeleteMapping("/blog/{blogId}")
    @ApiOperation("删除博客")
    public R deleteBlog(@PathVariable Long blogId) {
        return itBlogService.removeBlodById(blogId) ? R.ok() : R.error("删除失败");
    }

    @PostMapping("/blog/commentset")
    @ApiOperation("博客是否开启评论")
    public R changeBlogComment(@RequestBody TBlog comment){
        return  itBlogService.changeBlogComment(comment)? R.ok() : R.error("修改失败");
    }

    @GetMapping("/album/user")
    @ApiOperation("根据用户id查询照片")
    public R getimgeById(){
        //itBlogService.list(UserThreadLocalUtils.getWebUserId())
        return R.ok(itAlbumService.list(new LambdaQueryWrapper<TAlbum>().eq(TAlbum::getUserId,UserThreadLocalUtils.getWebUserId())));
    }


    @PutMapping("/blog/publish")
    @ApiOperation("文章发布/下架")

    public R chengePublish(@RequestBody TBlog tBlog){
        return  itBlogService.chengePublish(tBlog)? R.ok() : R.error("修改失败");
    }







}
